package com.example.hellodemo.widget;

import android.app.ActionBar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/*
*   自定义轮播图提示器
* */
import androidx.annotation.Nullable;

import com.example.hellodemo.R;

public class Indicator extends View {
    
    private Paint normalPaint; // 绘制正常提示器
    private Paint selectedPaint; // 绘制选中提示器
    private int normalColor = Color.parseColor("#00574B");  // 正常显示的颜色
    private int selectedColor = Color.parseColor("#008577"); // 选中显示的颜色
    private int radius = 20; // 圆点点半径
    private int padding = 20; // 圆点间隔

    private int currentIndex = 0;// 当前第几个选中

    public void setCount(int count) {
        this.count = count;
    }

    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    private int count = 5; // 圆点个数
    private int strokeWidth = 2; // 画笔宽度

    public void setNormalColor(int normalColor) {
        this.normalColor = normalColor;
    }

    public void setSelectedColor(int selectedColor) {
        this.selectedColor = selectedColor;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setPadding(int padding) {
        this.padding = padding;
    }

    // 代码构建方法    ==>  构建方法构建对象, set方法传属性, build方法配置 . 然后绘制图形
    public Indicator(Context context) {
        super(context);
    }

    // xml构造方法   在Styles.xml文件中 配置xml可设置的属性, 通过attrs 获取,给对象属性赋值
    public Indicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Indicator);
        count = typedArray.getInt(R.styleable.Indicator_Indicator_number,count);
        build();
    }
    // 初始化熟属性配置, 如果是代码构建则需在属性配置后 手动调用.
    public void build(){
        initPaint();
    }

    // view渲染绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        final float viewWidth = layoutParams.width;
        final float viewHeight = layoutParams.height;
        // 圆的起点x坐标 ,y坐标

        Float firstX = (viewWidth - count * 2 * radius - (count - 1) * padding) / 2 + radius;
        Float firstY = (viewHeight - 2 *radius) / 2 + radius;
        for (int i = 0 ; i< count ; i++){
            canvas.drawCircle(firstX + i * 2 * radius + i *padding ,firstY,radius,normalPaint);
        }
        canvas.drawCircle( firstX +  currentIndex * 2 * radius + currentIndex *padding ,firstY,radius,selectedPaint);
    }

    private void initPaint(){

        normalPaint = new Paint();
        normalPaint.setAntiAlias(true);
        normalPaint.setStyle(Paint.Style.STROKE);
        normalPaint.setStrokeWidth(strokeWidth);
        normalPaint.setColor(normalColor);

        selectedPaint = new Paint();
        selectedPaint.setAntiAlias(true);
        selectedPaint.setStrokeWidth(strokeWidth);
        selectedPaint.setStyle(Paint.Style.FILL);
        selectedPaint.setColor(selectedColor);
    }

    public void setOffset(float offset, float feifenbi){

    }
    public  void changeIndex(int index){
        currentIndex = index;
        // 重新绘制
        invalidate();
    }

}
